﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using Microsoft.VisualBasic;
using System.IO;

namespace Space_Conquest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);

            try
            {

                if ((args.Length > 0) && (args[0] == "/mapeditor"))
                {

                    Globals.bMapEditor = true;

                    if (args.Length > 1)
                        Globals.iMapEditorTilesX = Int32.Parse(args[1]);
                    else
                        Globals.iMapEditorTilesX = Int32.Parse(Interaction.InputBox("Enter the number of tiles wide the map should be.","TilesX", "",400,400));

                    if (Globals.iMapEditorTilesX < 20)
                    {
                        Globals.iMapEditorTilesX = 20;
                        MessageBox.Show("You set TilesX to less than 20, which is too small. It has been set to 20 for you.");
                    }

                    if (args.Length > 2)
                        Globals.iMapEditorTilesY = Int32.Parse(args[2]);
                    else
                        Globals.iMapEditorTilesY = Int32.Parse(Interaction.InputBox("Enter the number of tiles high the map should be.", "TilesY", "", 400, 400));

                    if (Globals.iMapEditorTilesY< 20)
                    {
                        Globals.iMapEditorTilesY = 20;
                        MessageBox.Show("You set TilesY to less than 20, which is too small. It has been set to 20 for you.");
                    }

                    if (args.Length > 3)
                        Globals.iMapEditorMapName = args[3];
                    else
                        Globals.iMapEditorMapName = Interaction.InputBox("Enter the name of the map class to make.", "Map Name", "", 400, 400);

                }

                // Start Application Normally
                StartApplication();

                // Start Unit Testing
                //UnitTesting.StartTesting();

            }
            catch (Exception ex) 
            {
                // Catches ANY exception that wasn't caught elsewhere
                if (MessageBox.Show(
                    "This program has encountered an error that it doesn't know what to do with.\n\n"+
                    "Would you like to see the details and attempt to understand what happened?\n"+
                    "(Warning: these details will look messy. Use them to look up what happened or ask somebody about what they mean)", "Unknown Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                {


                    MessageBox.Show(
                        String.Format(

                            "Error Message:\n"+
                            "-----------\n"+
                            "{0}\n\n\n"+
                            "Error Source:\n"+
                            "-----------\n"+
                            "{1}\n\n\n" +
                            "Error Stack Trace:\n"+
                            "-----------\n"+
                            "{2}\n"
                            ,
                            ex.Message,
                            ex.Source,
                            ex.StackTrace
                        ), "Error Details", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }

        /// <summary>
        /// Starts application normally, creates form and runs game loop
        /// </summary>
        static void StartApplication()
        {


            #if DEBUG

            Helpers.ClearLog();

            #endif

            // Tells loop below to keep running
            Globals.bRunning = true;

            // Creates our form in using(), 
            // Automatically calls .Dispose() on form after block
            using (GameForm form = new GameForm())
            {

                GameObjects.ObjectInitializer();
                Globals.GameStack.Push(new MainMenu());

                while (Globals.bRunning)
                {
                    Timer.Sleep();
                    form.Motion();
                    form.Render();
                    Application.DoEvents();
                }
            }

        }

    }

}
